package com.one.common.common.webview;

import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.one.common.utils.Logger;
import com.one.common.utils.StringUtils;

import java.net.URLDecoder;

/**
 * Author: zhulunjun
 * Time  : 2018/10/23 15:59
 * email : zhulunjun@qq.com
 * Describe :
 **/
public class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.startsWith("https://apis.map.qq.com/tools/locpicker")){
            return true;
        }
        super.shouldOverrideUrlLoading(view, url);
        if (StringUtils.isEmpty(url))
            return true;

        //以下内容  用于：页面中点击其他关键字搜索时，在最上端的EditText中添加hint
        Logger.d("地址 ", "url = " + url);
        try {
            Uri uri = Uri.parse(url);
            if(StringUtils.isBlank(uri.getQueryParameter("name"))){
                return true;
            }
            String name = URLDecoder.decode(uri.getQueryParameter("name"));
            String latng = uri.getQueryParameter("latng");
            String addr = URLDecoder.decode(uri.getQueryParameter("addr"));
            String city = URLDecoder.decode(uri.getQueryParameter("city"));

            Logger.d("地址 " + name + " " + latng + " " + addr + " " + city);

        }catch (Exception e){
            e.printStackTrace();
        }
//        Map<String, String> mapRequest = URLUtil.getRequestParamMap(url);
//        if(mapRequest!=null && mapRequest.size()!=0) {
//            String keyWord = mapRequest.get("keys");//获取关键字字段
//            Logger.d("myc", "keyWord = " + keyWord);
//            if (!StringUtils.isEmpty(keyWord)) {
//                try {
//                    String result = URLDecoder.decode(keyWord, "UTF-8");  //用Android自带的URLDecoder解析成中文
//                    setKeyword(result);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                }
//            }
//        }

        return true;
    }
}
